package com.weiyuwqj.admin.controller.admin.invest;

import com.weiyuwqj.admin.controller.admin.log.annotation.LogAnnotation;
import com.weiyuwqj.api.service.BusInvestCollectService;
import com.weiyuwqj.api.service.BusInvestService;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.model.convert.InvestAddtimeConvert;
import com.weiyuwqj.api.model.convert.ModelConvert;
import com.weiyuwqj.api.model.vo.BusItemCollectVo;
import com.weiyuwqj.api.util.DateUtils;
import com.weiyuwqj.api.util.ExcelUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by yxw on 2015/12/28.
 */
@Controller
@RequestMapping(value = "/admin/invest")
public class BusInvestCollectController {
    @Autowired
    BusInvestCollectService collectService;

    @Autowired
    BusInvestService busInvestService;
    /**
     * 显示
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getCollectList", method = RequestMethod.POST)
    public Object getCollectList(Paginator paginator, HttpServletRequest request){
        String commonCond = request.getParameter("commonCond");
        String itemId = request.getParameter("itemId");
        String userId = request.getParameter("userId");
        String name = request.getParameter("name");
        String mobile = request.getParameter("mobile");
        String collectStatus = request.getParameter("collectStatus");
        String collectStartTime = request.getParameter("collectStartTime");
        String collectEendTime = request.getParameter("collectEendTime");
        String addStartTime = request.getParameter("addStartTime");
        String addEndTime = request.getParameter("addEndTime");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("commonCond", commonCond);
        map.put("name", name);
        map.put("mobile", mobile);
        if(collectStatus!=null && collectStatus!=""){
           map.put("collectStatus", collectStatus);
        }
        map.put("itemId", itemId);
        map.put("userId", userId);
        map.put("collectStartTime", collectStartTime);
        map.put("collectEendTime", collectEendTime);
        map.put("addStartTime", addStartTime);
        map.put("addEndTime", addEndTime);
        return collectService.getList(paginator, map);
    }

    //导出
    @ResponseBody
    @RequestMapping("/excel/collect")
    @LogAnnotation(module ="投资管理/投资待收",description = "投资待收导出")
    public Object collectExcel(HttpServletRequest request,
                               String itemname,
                               String name,
                               String mobile,
                               String itemId,
                               String userId,
                               String collectStatus,
                               String collectStartTime,
                               String collectEendTime,
                               String addStartTime,
                               String addEndTime){

        String[] cArray={"项目名称","用户名","真实姓名","项目id","当前期数","总期数","应收总额","应收本金","应收利息","应收时间","已收总额","已收本金","已收利息","已收时间","添加时间"};
        String[] eArray={"ItemName","Username","Realname","ItemId","CollectCurrentPeriod","CollectTotalPeriod","CollectAmount",
                "CollectPrincipal","CollectInterest","CollectTime","ActualCollectAmount","ActualCollectPrincipal","ActualCollectInterest","ActualCollectTime","Addtime"};
        ModelConvert[] converts={null,null,null,null, null,null,null,null,null,new InvestAddtimeConvert(),null,null,null,new InvestAddtimeConvert(),new InvestAddtimeConvert()};

        List<String> cList= Arrays.asList(cArray);
        List<String> eList=Arrays.asList(eArray);
        List<ModelConvert> convertList=Arrays.asList(converts);
        Map map=new HashMap();
        map.put("itemname",itemname);
        map.put("name",name);
        map.put("mobile",mobile);
        map.put("itemId",itemId);
        map.put("userId",userId);
        map.put("collectStatus",collectStatus);
        map.put("collectStartTime",collectStartTime);
        map.put("collectEendTime",collectEendTime);
        map.put("addStartTime",addStartTime);
        map.put("addEndTime",addEndTime);

        List<BusItemCollectVo> contentList=collectService.getList(map);

        String serverDir=request.getServletContext().getRealPath("/")+"admin/export/";
        String excelFilePath=ExcelUtils.export("投资待收",cList,eList,convertList,contentList,serverDir);
        HashMap ret= Message.OK;
        ret.put("file","export/"+excelFilePath);
        return ret;
    }

    @ResponseBody
    @RequestMapping(value = "/listInvest", method = RequestMethod.POST)
    public Object listInvest(HttpServletRequest request, Paginator paginator) {
        String username = request.getParameter("username");
        String investStatus = request.getParameter("investStatus");
        String beginDate = request.getParameter("beginDate");
        String endDate = request.getParameter("endDate");
        String itemId = request.getParameter("itemId");
        String userId = request.getParameter("userId");
        String orderName = request.getParameter("orderName");
        String orderWay = request.getParameter("orderWay");
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(username)) {
            map.put("username", username);
        }
        if (StringUtils.isNotEmpty(userId)) {
            map.put("userId", userId);
        }
        if (StringUtils.isNotEmpty(itemId)) {
            map.put("itemId", itemId);
        }
        if (StringUtils.isNotEmpty(investStatus)) {
            map.put("investStatus", Integer.valueOf(investStatus));
        }
        if (StringUtils.isNotEmpty(beginDate)) {

            map.put("beginDate", DateUtils.getBeginTimeOfDate(beginDate));
        }
        if (StringUtils.isNotEmpty(endDate)) {
            map.put("endDate", DateUtils.getEndTimeOfDate(beginDate));
        }
        if (StringUtils.isNotEmpty(orderName)) {
            map.put("orderName", orderName);
        }
        if (StringUtils.isNotEmpty(orderWay)) {
            map.put("orderWay", orderWay);
        }
        return busInvestService.listInvest(paginator, map);
    }

//    @ResponseBody
//    @RequestMapping(value = "/doInvest", method = RequestMethod.POST)
//    public Object doInvest(Integer userId,Double amount,Integer itemId,@RequestParam(defaultValue ="0" )Integer id,Integer investType,Integer ticketId){
//
//        busInvestService.doInvest(userId,amount,itemId,id,investType,ticketId);
//        return Message.OK;
//    }

    @ResponseBody
    @RequestMapping(value = "/doCurrentInvest", method = RequestMethod.POST)
    public Object doCurrentInvest(Integer userId,Double amount,Integer itemId){
        busInvestService.investCurrent(userId,amount,itemId,0);
        return Message.OK;
    }

    @ResponseBody
    @RequestMapping(value = "/getCollectByUserId",method = RequestMethod.POST)
    public Object getCollectByUserId(Integer userId){
        return collectService.getCollectByUserId(userId);
    }

    /**
     * 根据用户id查询预期收益、已获得收益
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getProfitByUserId", method = RequestMethod.POST)
    public Object getProfitByUserId(Integer userId){
        return collectService.getProfitByUserId(userId);
    }

    @ResponseBody
    @RequestMapping(value="/getOne", method = RequestMethod.POST)
    public Object getOne(Integer itemId){
        Map<String,Object> number=busInvestService.getOne(itemId);
        return number;
    }




    //根据当天查询用户的投资记录4条
    @ResponseBody
    @RequestMapping(value="/getInvestListByUserIdAndTime", method = RequestMethod.POST)
    public Object getListByUserIdAndTime(Integer userId,String viewDate){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date=null;
        try {
            date=sdf.parse(viewDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date startDate= DateUtils.getDayStartTime(date);
        Date endDate= DateUtils.getNextDayStartTime(date);
        Map map=new HashMap();
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        map.put("userId",userId);
        return busInvestService.getListByUserIdAndTime(map);
    }

    //查询某天还款记录4条
    @ResponseBody
    @RequestMapping(value="/getRepayListByUserIdAndTime", method = RequestMethod.POST)
    public Object getRepayListByUserIdAndTime(Integer userId,String viewDate){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date=null;
        try {
            date=sdf.parse(viewDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date startDate= DateUtils.getDayStartTime(date);
        Date endDate= DateUtils.getNextDayStartTime(date);
        Map map=new HashMap();
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        map.put("userId",userId);
        return collectService.getListByUserIdAndTime(map);
    }


    //查询今天之后的还款记录4条
    @ResponseBody
    @RequestMapping(value="/getRepayListByUserIdAndNow", method = RequestMethod.POST)
    public Object getRepayListByUserIdAndNow(Integer userId){
        Map map=new HashMap();
        map.put("nowDate",new Date());
        map.put("userId",userId);
        return collectService.getListByUserIdAndNow(map);
    }

    //前台首页还款记录
    @ResponseBody
    @RequestMapping(value="/getMainRepayList", method = RequestMethod.POST)
    public Object getMainRepayList(Integer userId){
        Map map=new HashMap();
        map.put("userId",userId);
        return collectService.getMainRepayList(map);
    }

    //统计待收
    @ResponseBody
    @RequestMapping(value="/statCollect", method = RequestMethod.POST)
    public Object statCollect(HttpServletRequest request){
        String itemname = request.getParameter("itemname");
        String itemId = request.getParameter("itemId");
        String userId = request.getParameter("userId");
        String name = request.getParameter("name");
        String mobile = request.getParameter("mobile");
        String collectStatus = request.getParameter("collectStatus");
        String collectStartTime = request.getParameter("collectStartTime");
        String collectEendTime = request.getParameter("collectEendTime");
        String addStartTime = request.getParameter("addStartTime");
        String addEndTime = request.getParameter("addEndTime");
        String orderName = request.getParameter("orderName");
        String orderWay = request.getParameter("orderWay");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("itemname", itemname);
        map.put("name", name);
        map.put("mobile", mobile);
        if(collectStatus!=null && collectStatus!=""){
            map.put("collectStatus", collectStatus);

        }
        map.put("itemId", itemId);
        map.put("userId", userId);
        map.put("collectStartTime", collectStartTime);
        map.put("collectEendTime", collectEendTime);
        map.put("addStartTime", addStartTime);
        map.put("addEndTime", addEndTime);
        map.put("orderName",orderName);
        map.put("orderWay",orderWay);
        return collectService.statCollect(map);
    }


}
